package com.pine.app.module.security.oauth.provider.token;

import lombok.Getter;
import lombok.Setter;

import java.util.Date;
import java.util.Set;

/**
 * @author xiaoyuan
 * @create 2019/9/30 9:34
 **/
@Setter
@Getter
public class SimpleOAuth2AccessEntity implements OAuth2AccessToken {

    private static final long serialVersionUID = 2090466474249489203L;


    private String accessToken;

    private OAuth2RefreshToken oauth2RefreshToken;


    private String  refreshToken;

    private Date expireTime;

    private Set<String> scope;

    private String tokenType;

    public SimpleOAuth2AccessEntity(String accessToken){
        this.tokenType = "Bearer".toLowerCase();
        this.accessToken = accessToken;
    }

    @Override
    public String getRefreshToken() {
        return oauth2RefreshToken==null?"":oauth2RefreshToken.getValue();
    }

    @Override
    public boolean isExpire() {
        return getExpireTime().before(new Date());
    }

    public Integer getExpiresIn() {
        return this.expireTime != null?Long.valueOf((this.expireTime.getTime() - System.currentTimeMillis()) / 1000L).intValue():0;
    }
}
